<template>
	<el-main class="bg edit_wrap comtable_e">
		<el-form ref="form" :model="form" status-icon label-width="120px" v-if="is_view()">
		<el-row class="row_ce"> 
							<el-col v-if="user_group === '管理员' || $check_field('get','communication_title') || $check_field('add','communication_title') || $check_field('set','communication_title')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="交流标题" prop="communication_title">
												<el-input id="communication_title" v-model="form['communication_title']" placeholder="请输入交流标题"
							  v-if="user_group === '管理员' || (form['exchange_information_id'] && $check_field('set','communication_title')) || (!form['exchange_information_id'] && $check_field('add','communication_title'))" :disabled="disabledObj['communication_title_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','communication_title')">{{form['communication_title']}}</div>
											</el-form-item>
			</el-col>
								<el-col v-if="user_group === '管理员' || $check_field('get','pet_name') || $check_field('add','pet_name') || $check_field('set','pet_name')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="宠物名称" prop="pet_name">
												<el-input id="pet_name" v-model="form['pet_name']" placeholder="请输入宠物名称"
							  v-if="user_group === '管理员' || (form['exchange_information_id'] && $check_field('set','pet_name')) || (!form['exchange_information_id'] && $check_field('add','pet_name'))" :disabled="disabledObj['pet_name_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','pet_name')">{{form['pet_name']}}</div>
											</el-form-item>
			</el-col>
								<el-col v-if="user_group === '管理员' || $check_field('get','pet_species') || $check_field('add','pet_species') || $check_field('set','pet_species')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="宠物种类" prop="pet_species">
												<el-input id="pet_species" v-model="form['pet_species']" placeholder="请输入宠物种类"
							  v-if="user_group === '管理员' || (form['exchange_information_id'] && $check_field('set','pet_species')) || (!form['exchange_information_id'] && $check_field('add','pet_species'))" :disabled="disabledObj['pet_species_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','pet_species')">{{form['pet_species']}}</div>
											</el-form-item>
			</el-col>
								<el-col v-if="user_group === '管理员' || $check_field('get','delivery_users') || $check_field('add','delivery_users') || $check_field('set','delivery_users')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="送养用户" prop="delivery_users">
													<el-select v-if="user_group === '管理员' || (form['exchange_information_id'] && $check_field('set','delivery_users')) || (!form['exchange_information_id'] && $check_field('add','delivery_users'))" id="delivery_users" v-model="form['delivery_users']" :disabled="disabledObj['delivery_users_isDisabled']">
							<el-option v-for="o in list_user_delivery_users" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
									   :value="o['user_id']">
							</el-option>
						</el-select>
						<el-select v-else-if="$check_field('get','delivery_users')" id="delivery_users" v-model="form['delivery_users']" :disabled="true">
							<el-option v-for="o in list_user_delivery_users" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
									   :value="o['user_id']">
							</el-option>
						</el-select>
											</el-form-item>
			</el-col>
								<el-col v-if="user_group === '管理员' || $check_field('get','adoptive_users') || $check_field('add','adoptive_users') || $check_field('set','adoptive_users')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="领养用户" prop="adoptive_users">
																		<div v-if="user_group !== '管理员'">
							{{ get_user_session_adoptive_users(form['adoptive_users']) }}
							<!--<el-input id="business_name" v-model="form['adoptive_users']" placeholder="请输入领养用户"-->
							<!--v-if="user_group === '管理员' || (form['exchange_information_id'] && $check_field('set','adoptive_users')) || (!form['exchange_information_id'] && $check_field('add','adoptive_users'))" :disabled="disabledObj['adoptive_users_isDisabled']"></el-input>-->
							<!--<div v-else-if="$check_field('get','adoptive_users')">{{form['adoptive_users']}}</div>-->
							<el-select v-if="user_group === '管理员' || (form['exchange_information_id'] && $check_field('set','adoptive_users')) || (!form['exchange_information_id'] && $check_field('add','adoptive_users'))" id="adoptive_users" v-model="form['adoptive_users']" :disabled="disabledObj['adoptive_users_isDisabled']">
								<el-option v-for="o in list_user_adoptive_users" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
										   :value="o['user_id']">
								</el-option>
							</el-select>
							<el-select v-else-if="$check_field('get','adoptive_users')" id="adoptive_users" v-model="form['adoptive_users']" :disabled="true">
								<el-option v-for="o in list_user_adoptive_users" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
										   :value="o['user_id']">
								</el-option>
							</el-select>
						</div>
						<el-select v-else id="adoptive_users" v-model="form['adoptive_users']" :disabled="disabledObj['adoptive_users_isDisabled']">
							<el-option v-for="o in list_user_adoptive_users" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
									   :value="o['user_id']">
							</el-option>
						</el-select>
																</el-form-item>
			</el-col>
								<el-col v-if="user_group === '管理员' || $check_field('get','adoption_content') || $check_field('add','adoption_content') || $check_field('set','adoption_content')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="领养内容" prop="adoption_content">
								<el-input type="textarea" id="adoption_content" v-model="form['adoption_content']" placeholder="请输入领养内容"
						v-if="user_group === '管理员' || (form['exchange_information_id'] && $check_field('set','adoption_content')) || (!form['exchange_information_id'] && $check_field('add','adoption_content'))" :disabled="disabledObj['adoption_content_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','adoption_content')">{{form['adoption_content']}}</div>
							</el-form-item>
			</el-col>
								<el-col v-if="user_group === '管理员' || $check_field('get','adoption_notes') || $check_field('add','adoption_notes') || $check_field('set','adoption_notes')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="送养内容" prop="adoption_notes">
								<el-input type="textarea" id="adoption_notes" v-model="form['adoption_notes']" placeholder="请输入送养内容"
						v-if="user_group === '管理员' || (form['exchange_information_id'] && $check_field('set','adoption_notes')) || (!form['exchange_information_id'] && $check_field('add','adoption_notes'))" :disabled="disabledObj['adoption_notes_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','adoption_notes')">{{form['adoption_notes']}}</div>
							</el-form-item>
			</el-col>
					
	
	
	
	
	
	
		</el-row>
			<el-col :xs="24" :sm="12" :lg="8" class="el_form_btn_warp">
				<el-form-item v-if="$check_action('/exchange_information/view','set') || $check_action('/exchange_information/view','add') || $check_option('/exchange_information/table','examine')">
					<el-button type="primary" @click="submit()">提交</el-button>
					<el-button @click="cancel()">取消</el-button>
				</el-form-item>
				<el-form-item v-else>
					<el-button @click="cancel()">返回</el-button>
				</el-form-item>
			</el-col>

		</el-form>
	</el-main>
</template>

<script>
	import mixin from "@/mixins/page.js";

	export default {
		mixins: [mixin],
		data() {
			return {
				field: "exchange_information_id",
				url_add: "~/api/exchange_information/add?",
				url_set: "~/api/exchange_information/set?",
				url_get_obj: "~/api/exchange_information/get_obj?",
				url_upload: "~/api/exchange_information/upload?",

				query: {
					"exchange_information_id": 0,
				},

				form: {
								"communication_title":  '', // 交流标题
										"pet_name":  '', // 宠物名称
										"pet_species":  '', // 宠物种类
										"delivery_users": 0, // 送养用户
										"adoptive_users": 0, // 领养用户
										"adoption_content":  '', // 领养内容
										"adoption_notes":  '', // 送养内容
											"exchange_information_id": 0, // ID
						
				},
				disabledObj:{
								"communication_title_isDisabled": false,
										"pet_name_isDisabled": false,
										"pet_species_isDisabled": false,
										"delivery_users_isDisabled": false,
										"adoptive_users_isDisabled": false,
										"adoption_content_isDisabled": false,
										"adoption_notes_isDisabled": false,
										},

	
		
		
		
					// 用户列表
				list_user_delivery_users: [],
				
					// 用户列表
				list_user_adoptive_users: [],
						// 用户组
				group_user_adoptive_users: "",
				
		
	
			}
		},
		methods: {


	
	
			
	
			
	
			
	
				/**
			 * 获取送养用户用户列表
			 */
			async get_list_user_delivery_users() {
                // if(this.user_group !== "管理员" && this.form["delivery_users"] === 0) {
                //     this.form["delivery_users"] = this.user.user_id;
                // }
                var json = await this.$get("~/api/user/get_list?user_group=送养用户");
                if(json.result && json.result.list){
                    this.list_user_delivery_users = json.result.list;
                }
                else if(json.error){
                    console.error(json.error);
                }
			},
					get_user_delivery_users(id){
				var obj = this.list_user_delivery_users.getObj({"user_id":id});
				var ret = "";
				if(obj){
					if(obj.nickname){
						ret = obj.nickname;}
					else{
						ret = obj.username;
					}
				}
				return ret;
			},
			
	
				/**
			 * 获取领养用户用户列表
			 */
			async get_list_user_adoptive_users() {
                // if(this.user_group !== "管理员" && this.form["adoptive_users"] === 0) {
                //     this.form["adoptive_users"] = this.user.user_id;
                // }
                var json = await this.$get("~/api/user/get_list?user_group=领养用户");
                if(json.result && json.result.list){
                    this.list_user_adoptive_users = json.result.list;
                }
                else if(json.error){
                    console.error(json.error);
                }
			},
					/**
			 * 获取领养用户用户组
			 */
			async get_group_user_adoptive_users() {
							this.form["adoptive_users"] = this.user.user_id;
							var json = await this.$get("~/api/user_group/get_obj?name=领养用户");
				if(json.result && json.result.obj){
					this.group_user_adoptive_users = json.result.obj;
				}
				else if(json.error){
					console.error(json.error);
				}
			},
			get_user_session_adoptive_users(id){
				var _this = this;
				var user_id = {"user_id":id}
				var url = "~/api/"+_this.group_user_adoptive_users.source_table+"/get_obj?"
				this.$get(url, user_id, function(res) {
					if (res.result && res.result.obj) {
						var arr = []
						for (let key in res.result.obj) {
							arr.push(key)
						}
						var arrForm = []
									for (let key in _this.form) {
							arrForm.push(key)
						}
												_this.form["adoptive_users"] = id
									_this.disabledObj['adoptive_users' + '_isDisabled'] = true
						for (var i=0;i<arr.length;i++){
						  if (arr[i]!=='examine_state' && arr[i]!=='examine_reply') {
							for (var j = 0; j < arrForm.length; j++) {
							  if (arr[i] === arrForm[j]) {
								if (arr[i] !== "adoptive_users") {
			                      _this.form[arrForm[j]] = res.result.obj[arr[i]]
			                      _this.disabledObj[arrForm[j] + '_isDisabled'] = true
								  break;
								} else {
								  _this.disabledObj[arrForm[j] + '_isDisabled'] = true
								}
							  }
							}
						  }
						}
					}
				});
			},
					get_user_adoptive_users(id){
				var obj = this.list_user_adoptive_users.getObj({"user_id":id});
				var ret = "";
				if(obj){
					if(obj.nickname){
						ret = obj.nickname;}
					else{
						ret = obj.username;
					}
				}
				return ret;
			},
			
	
			
	
		
			/**
			 * 获取对象之前
			 * @param {Object} param
			 */
			get_obj_before(param) {
				var form = "";
												// 获取缓存数据附加
				form = $.db.get("form");
							$.push(this.form ,form);
														
				if(this.form && form){
					Object.keys(this.form).forEach(key => {
						Object.keys(form).forEach(dbKey => {
							// if(dbKey === "charging_standard"){
							// 	this.form['charging_rules'] = form[dbKey];
							// 	this.disabledObj['charging_rules_isDisabled'] = true;
							// };
							if(key === dbKey){
								this.disabledObj[key+'_isDisabled'] = true;
							}
						})
					})
				}
																		$.db.del("form");

				return param;
			},

			/**
			 * 获取对象之后
			 * @param {Object} json
			 * @param {Object} func
			 */
			get_obj_after(json, func){
																												

			},

			/**
			 * 提交前验证事件
			 * @param {Object} 请求参数
			 * @return {String} 验证成功返回null, 失败返回错误提示
			 */
			submit_check(param) {
																																																																					return null;
			},

			is_view(){
				var bl = this.user_group == "管理员";

				if(!bl){
					bl = this.$check_action('/exchange_information/table','add');
					console.log(bl ? "你有表格添加权限视作有添加权限" : "你没有表格添加权限");
				}
				if(!bl){
					bl = this.$check_action('/exchange_information/table','set');
					console.log(bl ? "你有表格添加权限视作有修改权限" : "你没有表格修改权限");
				}
				if(!bl){
					bl = this.$check_action('/exchange_information/view','add');
					console.log(bl ? "你有视图添加权限视作有添加权限" : "你没有视图添加权限");
				}
				if(!bl){
					bl = this.$check_action('/exchange_information/view','set');
					console.log(bl ? "你有视图修改权限视作有修改权限" : "你没有视图修改权限");
				}
				if(!bl){
					bl = this.$check_action('/exchange_information/view','get');
					console.log(bl ? "你有视图查询权限视作有查询权限" : "你没有视图查询权限");
				}

				console.log(bl ? "具有当前页面的查看权，请注意这不代表你有字段的查看权" : "无权查看当前页，请注意即便有字段查询权限没有页面查询权限也不行");

				return bl;
			},
			/**
			 * 上传文件
			 * @param {Object} param
			 */
			uploadimg(param) {
				this.uploadFile(param.file, "avatar");
			},

		},
		created() {
											this.get_list_user_delivery_users();
								this.get_list_user_adoptive_users();
					this.get_group_user_adoptive_users();
									},
	}
</script>

<style>
	.avatar-uploader .el-upload {
		border: 1px dashed #d9d9d9;
		border-radius: 6px;
		cursor: pointer;
		position: relative;
		overflow: hidden;
	}

	.avatar-uploader .el-upload:hover {
		border-color: #409EFF;
	}

	.avatar-uploader-icon {
		font-size: 28px;
		color: #8c939d;
		width: 178px;
		height: 178px;
		line-height: 178px;
		text-align: center;
	}

	.avatar {
		width: 178px;
		height: 178px;
		display: block;
	}




</style>
